<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      :status-icon="true"
      label-width="80px"
    >
      <el-form-item
        v-if="user.level < 1"
        label="所属区"
        prop="area"
      >
        <el-select
          v-model="form.area"
          placeholder="请选择所属区"
          clearable
        >
          <el-option
            v-for="item in initData.areaAtt"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="名称"
        prop="name"
      >
        <el-input
          v-model.trim="form.name"
          placeholder="请输入镇(街道)名称"
          clearable
          autocomplete="off"
        />
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="close">
        取 消
      </el-button>
      <el-button
        type="primary"
        @click="save"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
//手动添加
import { mapGetters } from "vuex";
export default {
  name: "TableEdit",
  props: ["fetchData"],
  computed: {
    ...mapGetters({
      user: "user/info",
    }),
  },
  data() {
    return {
      form: {
        name: "",
        area: ""
      },
      created: true,
      rules: {
        area: [{ required: true, trigger: "change", message: "请选择所属区" }],
        name: [{ required: true, trigger: "blur", message: "请输入镇(街道)名称" }],
      },
      title: "",
      dialogFormVisible: false,
      initData: {
        areaAtt: []
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = "镇(街道)信息添加";  
        this.created = true;
      } else {
        this.title = "镇(街道)信息编辑";
        this.form = Object.assign({}, row);
        this.form.area = row.area.id;
        this.created = false;
      }
      this.dialogFormVisible = true;
    },
    close() {
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
      this.setFile()
    },
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.created) {
            this.create();
          } else {
            this.edit();
          }
        } else {
          return false;
        }
      });
    },
    async create() {
      console.log(this.form.area)
      const res = await this.$http.post(`/town`, this.form);
      if (res.status == 201) {
        this.$baseMessage("新增成功", "success");
        this.$emit("fetchData");
        this.$refs["form"].resetFields();
        this.dialogFormVisible = false;
        this.form = this.$options.data().form;
      } else {
        this.$baseMessage("新增失败", "error");
      }
    },
    async edit() {
      const res = await this.$http.put(`/town/${this.form.id}`, this.form);
      if (res.status == 200) {
        this.$baseMessage("修改成功", "success");
        this.$emit("fetchData");
        this.$refs["form"].resetFields();
        this.dialogFormVisible = false;
        this.form = this.$options.data().form;
      } else {
        this.$baseMessage("修改失败", "error");
      }
    },
    async init() {
      const res = await this.$http.get("area");
      this.initData.areaAtt = res.data
      this.setFile()
    },
    setFile() {
      if(this.user.level > 0) {
        this.form.area = this.user.area.id
      }
    },
  },
};
</script>
